package FileControl.Source;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

public class CopyDir extends AbstractCopy {

	protected static boolean mkdir(File dir) {
		return dir.mkdir();
	}
	
	public static boolean basicCopyDir(File source, File dest) {
		List<File> errors = new LinkedList<File>();
		if(!source.isDirectory()) {
			println(source.getName() + ": is not a Directory!");
			return false;
		}
		else {
			if(!dest.exists()) {
				mkdir(dest);
			}
			for(File f : source.listFiles()) {
				if(!CopyFile.copyDestCheck(f, new File(dest.getPath() + "/" + f.getName())))
					errors.add(f);
			}
			if(errors.size() == 0) {
				return true;
			} else {
				println("These files had errors during copy and were NOT copied");
				for(File f : errors) {
					println("\t - " + f.getName());
				}
				return false;
			}
		}
	}
	
	public static boolean recursiveCopyDir(File source, File dest) {
		List<File> errors = new LinkedList<File>();
		if(!source.isDirectory()) {
			println(source.getName() + ": is not a Directory!");
			return false;
		}
		else {
			if(!dest.exists()) {
				mkdir(dest);
			}
			for(File f : source.listFiles()) {
				if(f.isDirectory()) {
					if(!recursiveCopyDir(f,new File(dest.getPath() + "/" + f.getName())))
						errors.add(f);
				}
				else {
					if(!CopyFile.copyDestCheck(f, new File(dest.getPath() + "/" + f.getName())))
						errors.add(f);
				}
			}
			
			if(errors.size() == 0) {
				return true;
			} else {
				println("These files/folders had errors during copy and were NOT copied");
				for(File f : errors) {
					if(f.isDirectory())
						println("\t + " + f.getName());
					else
						println("\t - " + f.getName());
				}
				return false;
			}
		}
	}
}
